package com.ibeeking.found.upms.b.rest.service.impl;

import com.ibeeking.found.upms.b.rest.service.ITenantRolePermissionService;
import com.ibeeking.found.upms.b.rest.service.ITenantRoleService;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.upms.service.common.bo.TenantRolePermissionBO;
import com.ibeeking.found.upms.service.common.dos.TenantRolePermissionDO;
import com.ibeeking.found.upms.service.common.param.PermissionRoleParam;
import com.ibeeking.found.upms.service.common.param.RolePermissionParam;
import com.ibeeking.found.upms.service.mapper.TenantRolePermissionMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @project: found-upms
 * @description: 角色权限接口实现类
 * @Author ibeeking
 * @create: 2020-12-05 15:19:57
 **/
@Service
public class TenantRolePermissionServiceImpl extends ServiceImpl<TenantRolePermissionMapper, TenantRolePermissionDO> implements ITenantRolePermissionService {

    @Resource
    private TenantRolePermissionMapper tenantRolePermissionMapper;
    @Resource
    private ITenantRoleService roleServiceImpl;

    @Override
    public List<TenantRolePermissionBO> queryRolePermissionByRoleIds(List<Long> roleIds) {
        List<TenantRolePermissionDO> rolePermissionDos = tenantRolePermissionMapper.selectList(new LambdaQueryWrapper<>(TenantRolePermissionDO.class).in(TenantRolePermissionDO::getRoleId, roleIds));
        if (Collections3.isEmpty(rolePermissionDos)) {
            return Collections3.emptyList();
        }
        return JsonUtils.jsonToList(rolePermissionDos, TenantRolePermissionBO.class);
    }

    @Override
    public List<TenantRolePermissionBO> queryRolePermissionByRoleId(Long roleId) {
        List<TenantRolePermissionDO> rolePermissionDOS = tenantRolePermissionMapper.selectList(new LambdaQueryWrapper<>(TenantRolePermissionDO.class).eq(TenantRolePermissionDO::getRoleId, roleId));
        if (Collections3.isEmpty(rolePermissionDOS)) {
            return Collections3.emptyList();
        }
        return JsonUtils.jsonToList(rolePermissionDOS, TenantRolePermissionBO.class);
    }

    @Override
    public List<TenantRolePermissionBO> queryRolePermissionByPermissionId(Long permissionId) {
        List<TenantRolePermissionDO> rolePermissionDOS = tenantRolePermissionMapper.selectList(new LambdaQueryWrapper<>(TenantRolePermissionDO.class).eq(TenantRolePermissionDO::getPermissionId, permissionId));
        if (Collections3.isEmpty(rolePermissionDOS)) {
            return Collections3.emptyList();
        }
        return JsonUtils.jsonToList(rolePermissionDOS, TenantRolePermissionBO.class);
    }

    @Transactional(rollbackFor = {BusinessException.class})
    @Override
    public Boolean modifyRolePermissionByRoleId(CurrentUser currentUser, RolePermissionParam param) {
        Long administratorId = roleServiceImpl.selectAdministratorId();
        if (administratorId.equals(param.getRoleId())) {
            return true;
        }
        tenantRolePermissionMapper.delete(new LambdaQueryWrapper<>(TenantRolePermissionDO.class)
                .eq(TenantRolePermissionDO::getRoleId, param.getRoleId())
        );

        if (null == param.getPermissionIds() || 0 == param.getPermissionIds().length) {
            return true;
        }
        List<Long> paramIds = Arrays.asList(param.getPermissionIds());
        //新增关系
        List<TenantRolePermissionDO> saves = new ArrayList<>();
        paramIds.stream().forEach(key -> {
            TenantRolePermissionDO rolePermissionDO = new TenantRolePermissionDO();
            rolePermissionDO.setRoleId(param.getRoleId());
            rolePermissionDO.setPermissionId(key);
            saves.add(rolePermissionDO);
        });
        tenantRolePermissionMapper.insertBatchSomeColumn(saves);
        return true;
    }

    @Transactional(rollbackFor = {BusinessException.class})
    @Override
    public Boolean modifyRolePermissionByPermissionId(CurrentUser currentUser, PermissionRoleParam param) {
        tenantRolePermissionMapper.delete(new LambdaQueryWrapper<>(TenantRolePermissionDO.class)
                .eq(TenantRolePermissionDO::getPermissionId, param.getPermissionId())
        );

        Long administratorId = roleServiceImpl.selectAdministratorId();
        if (null == param.getRoleIds() || 0 == param.getRoleIds().length) {
            return true;
        }
        List<Long> paramIds = Arrays.asList(param.getRoleIds());
        paramIds = paramIds.parallelStream().filter(roleId -> !administratorId.equals(roleId)).collect(Collectors.toList());
        //新增
        List<TenantRolePermissionDO> saves = new ArrayList<>();
        paramIds.parallelStream().forEach(key -> {
            TenantRolePermissionDO rolePermissionDO = new TenantRolePermissionDO();
            rolePermissionDO.setRoleId(key);
            rolePermissionDO.setPermissionId(param.getPermissionId());
            saves.add(rolePermissionDO);
        });
        tenantRolePermissionMapper.insertBatchSomeColumn(saves);
        return true;
    }
}
